﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace WebLib
{
    public static class PagingHelper
    {
        public static HtmlString PageLinkers(this System.Web.Mvc.HtmlHelper html, int currentPage, int totalPages, Func<int, string> pageUrl)
        {
            return PageLinkers(html, 10, currentPage, totalPages, pageUrl);
        }

        public static HtmlString PageLinkers(this System.Web.Mvc.HtmlHelper html, int renderPageCount, int currentPage, int totalPages, Func<int, string> pageUrl)
        {
            var roopCount = renderPageCount > totalPages ? totalPages : renderPageCount;

            int startPage = 1;

            while (startPage + roopCount <= currentPage)
            {
                startPage = startPage + roopCount;
            }

            string pagingHtml = GetPagingHtml2(currentPage, totalPages, pageUrl, roopCount, startPage);

            return new HtmlString(pagingHtml);
        }

        public static string GetPagingHtml1(int currentPage, int totalPages, Func<int, string> pageUrl, int roopCount, int startPage)
        {
            var result = new StringBuilder();

            for (var i = startPage; i < startPage + roopCount; i++)
            {
                if (i <= totalPages)
                {
                    TagBuilder tag = null;

                    if (i == currentPage)
                    {
                        tag = new TagBuilder("strong") { InnerHtml = i.ToString() };
                    }
                    else
                    {
                        tag = new TagBuilder("a");
                        tag.MergeAttribute("href", pageUrl(i));
                        tag.InnerHtml = i.ToString();
                    }

                    if (i == 1)
                        tag.AddCssClass("frst");

                    result.AppendLine(tag.ToString());
                }
            }

            return result.ToString();
        }

        public static string GetPagingHtml2(int currentPage, int totalPages, Func<int, string> pageUrl, int roopCount, int startPage)
        {
            var result = new StringBuilder();

            for (var i = startPage; i < startPage + roopCount; i++)
            {
                if (i > totalPages) continue;

                string tag;

                if (i == currentPage) tag = "<li class=\"active\"><a href=\"#\" onclick=\"return false;\" >" + i + "</a></li>";
                else tag = "<li><a href=\"" + pageUrl(i) + "\">" + i + "</a></li>";

                result.AppendLine(tag);
            }

            return result.ToString();
        }

        public static int GetPageCount(int totalCount, int pageSize)
        {
            return totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1;
        }

        public static string GetParameterString()
        {
            var queryStringCollection = HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
            queryStringCollection.Remove("page");

            if (!string.IsNullOrEmpty(queryStringCollection.ToString()))
                return "&" + queryStringCollection;
            else
                return string.Empty;
        }
    }

    public class PagingVM
    {
        private int _currentPage;
        public int CurrentPage
        {
            get
            {
                if (_currentPage < 1)
                    return _currentPage = 1;

                return _currentPage;
            }
            set
            {
                _currentPage = value;
            }
        }

        private int _pageCount;
        public int PageCount
        {
            get
            {
                if (_pageCount < 1)
                    return _pageCount = 1;

                return _pageCount;
            }
            set
            {
                _pageCount = value;
            }
        }

        private int _pageSize;
        public int PageSize
        {
            get
            {
                if (_pageSize < 1)
                    return _pageSize = 10;

                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }

        private string _parameters;
        public string Parameters
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_parameters))
                    return _parameters = string.Empty;

                return _parameters;
            }
            set
            {
                _parameters = value;
            }
        }

        private int _renderPageCount;
        public int RenderPageCount
        {
            get
            {
                if (_renderPageCount < 1)
                    return _renderPageCount = 10;

                return _renderPageCount;
            }
            set
            {
                _renderPageCount = value;
            }
        }
    }
}
